﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using CFM.DTO;
using CFM.DAL;

namespace CFM.API
{
    public class UserAPI
    {
        private DataHelper _helper = null;
        public UserAPI()
        {
            _helper = new DataHelper();
        }

        //public List<User> GetAll()
        //{
        //    string sql = "select * from [User]";
        //    SqlDataReader dr = _helper.ExcuteDataReader(sql, null);
        //    return _helper.MapDataReaderToObject<User>(dr);
        //}
        public List<User> GetAllUser()
        {
            List<User> listuser = new List<User>();
            string sql = "select *,B.RoleName from [User] A join Role B on A.RoleId=B.RoleId order by UpdateDate desc";
            SqlDataReader dr = _helper.ExcuteDataReader(sql, null);
            if (dr != null)
            {
                while (dr.Read())
                {
                    User us = new User();
                    us.UserId = dr["UserID"].ToString();
                    us.RoleId = dr["RoleId"].ToString();
                    us.FullName = dr["FullName"].ToString();
                    us.Password = dr["Password"].ToString();
                    us.Email = dr["Email"].ToString();
                    us.Phone = dr["Phone"].ToString();
                    us.UpdateDate = Convert.ToDateTime(dr["UpdateDate"].ToString());
                    us.RoleName = dr["RoleName"].ToString();
                    listuser.Add(us);
                }
                _helper.DisConnect();
            }
            return listuser;
        }
        public bool Create(User user)
        {
            string sql = "insert into [User](Userid,FullName,Password,Email,Phone,RoleId,UpdateDate) values(@Userid,@FullName,@Password,@Email,@Phone,@RoleId,@UpdateDate)";
            SqlParameter[] prs = 
            {
                new SqlParameter("@Userid",user.UserId),
                new SqlParameter("@FullName",user.FullName),
                new SqlParameter("@Password",user.Password),
                new SqlParameter("@Email",user.Email),
                new SqlParameter("@Phone",user.Phone),
                new SqlParameter("@RoleId",user.RoleId),
                new SqlParameter("@UpdateDate",user.UpdateDate)
            };
            return _helper.ExcuteNonQuery(sql, prs);
        }

        public bool Update(User user)
        {
            string sql = "update [User] set FullName = @FullName,Password= @Password,Email=@Email,Phone=@Phone,RoleId=@RoleId,UpdateDate=@UpdateDate where Userid = @Userid";
            SqlParameter[] prs = 
            {
                new SqlParameter("@Userid",user.UserId),
                new SqlParameter("@FullName",user.FullName),
                new SqlParameter("@Password",user.Password),
                new SqlParameter("@Email",user.Email),
                new SqlParameter("@Phone",user.Phone),
                new SqlParameter("@RoleId",user.RoleId),
                new SqlParameter("@UpdateDate",user.UpdateDate)
            };
            return _helper.ExcuteNonQuery(sql, prs);
        }

        public bool Delete(string userID)
        {
            string sql = "delete [User] where Userid = @Userid";
            SqlParameter[] prs = 
            {
                new SqlParameter("@Userid",userID)
            };
            return _helper.ExcuteNonQuery(sql, prs);
        }

        public User GetUserByIdAndPass(string userId, string passWord)
        {
            string sql = "select * from [User] where userid= @userid and password = @password";
            SqlParameter[] prs = 
            {
                new SqlParameter("@userid",userId),
                new SqlParameter("@password",passWord)
            };
            SqlDataReader dr = _helper.ExcuteDataReader(sql, prs);
            var result =  _helper.MapDataReaderToObject<User>(dr);
            if (result.Count > 0)
                return result.FirstOrDefault();
            else
                return new User();
        }
        public bool ChangePass(User user)
        {
            string sqlChagepass = "update [User] set Password=@Password where UserID=@UserID";
            SqlParameter[] pr ={
                                   new SqlParameter("@UserID", user.UserId),
                              new SqlParameter("@Password",user.Password)
                              };
            return _helper.ExcuteNonQuery(sqlChagepass, pr);
        }
    
    }
}
